﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Reflection;


namespace NLE
{
    public partial class ImageAddMultiple : Form
    {
        public MainForm mainForm { get; set; }

        String tempName = String.Empty;

        String tempDirectory = String.Empty;

        public ImageAddMultiple()
        {
            InitializeComponent();

            // Also, update our group select box
            //cmbGroup.Items.Add(cmbGroup.Text);


        }

        public void UpdateGroups()
        {
            
            foreach (var Node in mainForm.treeImages.Nodes)
            {

                cmbGroup.Items.Add(Node.ToString().Replace("TreeNode: ", ""));
            }
        }


        private void btnAddToLevel_Click(object sender, EventArgs e)
        {
            ProcessDirectory();
        }

        private void ProcessDirectory()
        {
            this.Cursor = Cursors.WaitCursor;

            btnAddToLevel.Enabled = false;
            btnOpenDirectory.Enabled = false;
            cmbGroup.Enabled = false;


            //FileInfo[] finfo = s.GetFiles("*.png");

            string lookfor = "*.BMP;*.GIF;*.EXIF;*.JPG;*.PNG;*.TIFF";
            string[] extensions = lookfor.Split(new char[] { ';' });

            ArrayList myfileinfos = new ArrayList();
            DirectoryInfo di = new DirectoryInfo(tempDirectory);

            foreach (string ext in extensions)
            {
                myfileinfos.AddRange(di.GetFiles(ext));
            }

            FileInfo[] allfileinfo = (FileInfo[])myfileinfos.ToArray(typeof(FileInfo));


            foreach (var file in allfileinfo)
            {
                tempName = Path.GetFileNameWithoutExtension(file.FullName);

                mainForm.AddImageToLibrary(tempName, file.FullName, cmbGroup.Text);
            }

            

            mainForm.UpdateTreeView();

            Close();
        }

        private void btnOpenDirectory_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();


            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                tempDirectory = folderDialog.SelectedPath;

                if (!String.IsNullOrEmpty(tempDirectory))
                {
                    btnAddToLevel.Enabled = true;
                }
                else
                {
                    btnAddToLevel.Enabled = false;
                }
            }
        }
    }
}
